package pl.prv.polanie.client.filemanager;


import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;



public class BookmarkManagerController extends SimplePanel {
	
	Directory dir = new Directory();
//	VerticalPanel vp = new VerticalPanel();
	
	public BookmarkManagerController() {
		
		dir.setDirName("Linki");
		setWidget(dir);
		
		
		ResponseTextHandler rth = new ResponseTextHandler() {
			public void onCompletion(String responseText) {
				Document doc = XMLParser.parse(responseText);
				XMLParser.removeWhitespace(doc);
//				Window.alert("B " +  doc);
				parseBookmarks(doc);
				
////				dir.setDirName("mars.iti.pk.edu.pl/~swoosh/");
////				dir.setPath("http://mars.iti.pk.edu.pl/~swoosh/");
//
////				dir.setDirName("dirName");
////				dir.setPath("path");
//				
//				dir.setDirName("Files");
//				dir.setPath("http://localhost/gwt/");
//				
//				try {
//					JSONValue val = JSONParser.parse(responseText);
//					parseOneLevel(val, dir);
//				} catch (NullPointerException e) {
//					Window.alert("NPE " + e.toString() );
//				} catch (Exception e) {
//					Window.alert("EXC " + e.toString());
//				}
//				dir.setCollapsedRecursively(false);
			}
		};
		
		HTTPRequest.asyncGet("bookmarks.xml", rth);
//		HTTPRequest.asyncGet("http://localhost/gwt/dane2.php", rth);
	}
	
	private void parseBookmarks(final Document doc) {
		NodeList nl = doc.getChildNodes();
		for (int i = 0; i < nl.getLength(); i++) {
			Node node = nl.item(i);
			if (node.getNodeName().equals("bookmarks")) {
				
				
				NodeList bookmarkArray = node.getChildNodes();
				//WidgetsProject.log("mam bookamrs, children: " + bookmarkArray.getLength());

				for (int j = 0; j < bookmarkArray.getLength(); j++) {
					Node bookmark = bookmarkArray.item(j);
					
//					Label link = new Label();
					String titleText = "";
					String urlText = "";
					
					
					NodeList bmNodes = bookmark.getChildNodes();
					//WidgetsProject.log("mam bookmark, children: " + bmNodes.getLength());
					
					for (int k = 0; k < bmNodes.getLength(); k++) {
						Node n = bmNodes.item(k);
						if (n.getNodeName().equals("title")) {
							titleText = n.getFirstChild().getNodeValue();
						} else if (n.getNodeName().equals("url")) {
							urlText = n.getFirstChild().getNodeValue();
						}
					}
					Link link = new Link(titleText, urlText);
					
					dir.addChild(link);
				}
				
			}
//			vp.add(new Label(""+node.getNodeName()));
		}
		
	}
	
	private void parseOneLevel(final JSONValue jsonVal, final Directory parentDir) {
		try {
			JSONArray arr = jsonVal.isArray();
			
			for (int i = 0; i < arr.size(); i++) {
				JSONValue v = arr.get(i);
				JSONObject obj = v.isObject();
				
				JSONValue vName = obj.get("name");
				JSONString name = vName.isString();
				
				JSONValue vContent = obj.get("content");
				
				//katalog
				// && (vContent.isNull() == null)  z tym katalogi byly biale te zagn
				if (vContent != null ) {
					final Directory d = new Directory();
					d.setDirName(name.stringValue());
					d.setPath(parentDir.getPath() + name.stringValue());
					parentDir.addChild(d);
					
					if (vContent.isNull() == null) {
						parseOneLevel(vContent, d);	
					}
					
				}
				//plik
				else {
					final File f = new File(parentDir.getPath(), name.stringValue());
					parentDir.addChild(f);
				}
			}
			
		} catch (NullPointerException e) {
			Window.alert("NullPointerException " + e.toString() );
		} catch (Exception e) {
			Window.alert("Exception " + e.toString());
		}
	}



	public Directory getDir() {
		return dir;
	}

}
